package com.example.workbench.service.impl;

import com.example.workbench.common.api.CommonResult;
import com.example.workbench.common.command.AddUserCommand;
import com.example.workbench.common.command.LoginUserCommand;
import com.example.workbench.common.config.shiro.JwtUtils;
import com.example.workbench.common.config.shiro.RedisUtil;
import com.example.workbench.common.utils.CodeUtils;
import com.example.workbench.common.utils.DecryptUtils;
import com.example.workbench.common.vo.CreateCodeVo;
import com.example.workbench.common.vo.KaptchaCodeVo;
import com.example.workbench.entity.DbUser;
import com.example.workbench.mapper.DbUserMapper;
import com.example.workbench.service.IDbUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 
 * @since 2024-06-26
 */
@Service
public class DbUserServiceImpl extends ServiceImpl<DbUserMapper, DbUser> implements IDbUserService {
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private JwtUtils jwtUtils;
    @Override
    public CommonResult register(AddUserCommand command) throws Exception {
//        判断是否已经存在
        DbUser user = getBaseMapper().getUserByUserName(command.getUsername());
        if (null != user) {
            return CommonResult.failed("用户已存在！");
        }
        DbUser addUser = new DbUser();
//        String newPassword = DecryptUtils.decrypt(command.getPassword());
        addUser.setUsername(command.getUsername());
        addUser.setPassword(command.getPassword());
        addUser.setPhone(null);
        addUser.setAvatar(null);
        addUser.setNickName(command.getUsername());
        int insert = getBaseMapper().insert(addUser);
        if (insert == 0) {
            return CommonResult.failed("注册失败！");
        }
        return CommonResult.success("注册成功");
    }

    @Override
    public CommonResult doLogin(LoginUserCommand command) throws Exception {
        //        判断是否已经存在
        DbUser user = getBaseMapper().getUserByUserName(command.getUsername());
        if (null == user) {
            return CommonResult.failed("用户不存在！");
        }
        // 验证验证码
        System.out.println("command.getUuId()-------------"+command.getUuId());
//        Object object = redisUtil.get(command.getUuId());
//        if (null == object) {
//            return CommonResult.failed("验证码已失效！");
//        }
//        System.out.println("验证码"+object.toString());
        if (!command.getCode().equals("1234")) {
            return CommonResult.failed("验证码错误！");
        }
        //解密
//        String newPassword = DecryptUtils.decrypt(command.getPassword());
        if (!command.getPassword().equals(user.getPassword())) {
            System.out.println("command.getPassword()--"+command.getPassword());
            System.out.println("user.getPassword()--"+user.getPassword());
            return CommonResult.failed("密码错误！");
        }
//        生成用户基本信息以及token信息
        Map<String,String> userMap = new HashMap<>();
        String token = jwtUtils.createToken(user.getUsername(),user.getId(), 60);
        userMap.put("token",token);
        return CommonResult.success(userMap);
    }

    @Override
    public CommonResult getCode() throws IOException {
//        生成UUid 和 imageUrl
        UUID uuid = UUID.randomUUID();
        KaptchaCodeVo kaptchaCodeVo = CodeUtils.createCode();
//        存到redis
        redisUtil.set(uuid.toString(), kaptchaCodeVo.getText());
        redisUtil.expire(uuid.toString(),1, TimeUnit.MINUTES);
        System.out.println(kaptchaCodeVo.getImgUrl());
        System.out.println(kaptchaCodeVo.getText());
        CreateCodeVo vo = new CreateCodeVo(uuid,kaptchaCodeVo.getImgUrl());
        return CommonResult.success(vo);
    }
}
